<template>
  <el-card header="个人信息" shadow="never">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" style="margin-top:20px;">
      <el-form-item label="用户头像" prop="logoUrl">
        <el-upload
            ref="upload"
            :limit="1"
            accept=".jpg, .png"
            :action="upload.url"
            :headers="upload.headers"
            :file-list="upload.fileList"
            :on-preview="handleFileUploadProgress"
            :on-success="handleFileSuccess"
            :on-remove="handleRemoveFile"
            :auto-upload="false">
          >
          <template #trigger>
            <el-button type="primary">选取文件</el-button>
          </template>
          <el-button class="ml-3" type="success" @click="handleSubmitUpload">
            上传文件
          </el-button>
          <template #tip>
            <div class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </template>
        </el-upload>
      </el-form-item>
      <el-form-item label="账号" prop="username">
        <el-input v-model="form.username" disabled></el-input>
        <div class="el-form-item-msg">账号信息用于登录，系统不允许修改</div>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone" disabled></el-input>
        <div class="el-form-item-msg">手机号可用于登录，系统不允许修改</div>
      </el-form-item>
      <el-form-item label="邮箱号" prop="email">
        <el-input v-model="form.email" disabled></el-input>
        <div class="el-form-item-msg">邮箱号可用于登录，系统不允许修改</div>
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="form.nickName"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="form.sex" placeholder="请选择">
          <el-option label="保密" :value="2"></el-option>
          <el-option label="男" :value="1"></el-option>
          <el-option label="女" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="saveing" @click="save">保存</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
module.exports = {
  data() {
    return {
      // 保存遮罩层
      saveing: false,
      userId: this.$TOOL.data.get("APP_USER_INFO").userId,
      form: {
        username: '',
        phone: '',
        email: '',
        avatar: '',
        nickName: '',
        sex: '',
        remark: '',
      },
      rules: {
        username: [
          {required: true, message: '请输入用户账户', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入联系电话', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入联系邮箱', trigger: 'blur'}
        ],
        nickName: [
          {required: true, message: '请输入用户昵称', trigger: 'blur'}
        ],
        sex: [
          {required: true, message: '请选择用户性别', trigger: 'blur'}
        ],
      },
      // 上传参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: {Authorization: this.$TOOL.getJti()},
        // 上传的地址
        url: this.$CONFIG.API_URL + "/platform/file/uploadImage",
        // 上传的文件列表
        fileList: []
      },
    }
  },
  created() {
    this.getInfo();
  },
  methods: {
    getInfo() {
      this.$API.user.getUserById.get(this.userId).then(response => {
        if (response.status) {
          this.form = response.data;
        } else {
          this.$message.warning(response.msg);
        }
      });
    },
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.saveing = true;
          this.$API.user.editUser.put(this.form).then(response => {
            if (response.status) {
              this.$message.success(response.msg);
              this.saveing = false;
              this.getInfo();
              this.$emit('getInfo');
            } else {
              this.$alert(response.msg, "提示", {type: 'error'});
              this.saveing = false;
            }
          });
        }
      });
    },
    // 文件提交处理
    handleSubmitUpload() {
      this.$refs.upload.submit();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.isUploading = false;
      this.form.avatar = response.data.fileUrl;
      this.$message.success(response.msg);
    },
    handleRemoveFile() {
      this.imgUrl = null
    }
  }
}
</script>

<style>
</style>
